<template>
  <el-form
    v-loading="loading"
    :model="ruleForm"
    status-icon
    :rules="rules"
    ref="ruleForm"
    label-width="100px"
    class="demo-ruleForm">
    <el-form-item label="菜单名称" prop="name">
      <el-input  v-model="ruleForm.name" ></el-input>
    </el-form-item>
    <el-form-item label="路由" prop="path">
      <el-input  v-model="ruleForm.path" ></el-input>
    </el-form-item>
    <el-form-item label="图标" prop="icon">
      <el-input  v-model="ruleForm.icon" ></el-input>
    </el-form-item>
    <el-form-item label="权限标识" prop="permissionId">
      <el-input  v-model="ruleForm.permissionId" ></el-input>
    </el-form-item>
    <el-form-item label="菜单类型" prop="category">
      <el-select v-model="ruleForm.category" placeholder="请选择" style="width: 320px">
        <el-option
          v-for="(item,index) in menuType"
          :key="index"
          :label="item.dictValue"
          :value="item.dictKey">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="按钮类型" prop="actionName" v-if="ruleForm.category === '2'">
      <el-select v-model="ruleForm.action" placeholder="请选择" style="width: 320px">
        <el-option
          v-for="(item,index) in buttonType"
          :key="index"
          :label="item.dictValue"
          :value="item.dictKey">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="父菜单" prop="parentId">
      <el-cascader
        style="width: 320px"
        @change="change"
        :options="menuTree"
        :props="{ checkStrictly: true ,children: 'children',label:'name',value:'id'}"
        clearable></el-cascader>
    </el-form-item>
    <el-form-item label="状态" prop="status">
      <el-select v-model="ruleForm.status" placeholder="请选择" style="width: 320px">
        <el-option
          label="启用"
          :value="1">
        </el-option>
        <el-option
          label="停用"
          :value="0">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="排序" prop="sort">
      <el-input-number v-model="ruleForm.sort" :step="2"></el-input-number>
    </el-form-item>
    <el-form-item style="margin-top: 40px;margin-left: -28px">
      <el-button type="primary" @click="submitForm('ruleForm')" style="width: 320px;height: 41px;">提交</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { ElMessage } from 'element-plus'

export default {
  name: 'DataAdd',
  data(){
    return{
      ruleForm:{},
      loading:false,
      menuTree:[],
      menuType:[],
      buttonType:[],
      rules:{
        name: [
          { required: true, message: '请输入菜单名称', trigger: 'blur' },
        ],
        category: [
          { required: true, message: '请输入菜单类型', trigger: 'blur' },
        ],
        status: [
          { required: true, message: '请选择菜单状态', trigger: 'blur' },
        ],
        sort: [
          { required: true, message: '请输入角色排序', trigger: 'blur' },
        ],
      }
    }
  },
  mounted () {
    //获取菜单树
    this.getMenuTree();
    //获取数据字典
    this.getDateDict()
  },
  methods:{
    //提交数据
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.loading = true
          this.$http.post('/api/menu',this.ruleForm).then(res => {
            this.loading = false
            ElMessage({
              type: 'success',
              showClose: true,
              message: res.data.message,
            })
            //触发事件
            this.$emit('tao')
          }).catch(() => this.loading = false)
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    //获取菜单树，不包括按钮
    getMenuTree(){
      this.$http.get('/api/menu/tree?category=1').then(res =>{
        this.menuTree = res.data.data;
      })
    },
    //父菜单级联选择器，获取的的全路径
    change(val){
      //拼接路径
      this.ruleForm.parentId = val[val.length - 1]
    },
    //获取字典
    getDateDict(){
      this.$http.get('/api/dict/list?codeList='+ 'menu_type,button_type').then(res =>{
        this.menuType = res.data.data.menu_type
        this.buttonType = res.data.data.button_type
      })
    }
  }
}
</script>

<style scoped>

</style>
